﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace FDeal
{
    public class DonHangDAO : DataProvider
    {
        public DonHangDAO()
        { 
        }

        public DonHangDTO[] getDsDonHang()
        {
            connect();
            string query = "SELECT * FROM DONHANG";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            DonHangDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new DonHangDTO[n];
            for (i = 0; i < n; i++)
            {
                object lop = GetDataFromDataRow(dt, i);
                arr[i] = (DonHangDTO)lop;
            }
            return arr;
        }



        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            DonHangDTO dh = new DonHangDTO();
            dh.Iddonhang = dt.Rows[i]["IDDONHANG"].ToString();
            dh.Idthanhtoan = dt.Rows[i]["IDTHANHTOAN"].ToString();
            dh.Diachinhan = dt.Rows[i]["DIACHINHAN"].ToString();
            dh.Duong = dt.Rows[i]["DUONG"].ToString();
            dh.Tinhthanh = dt.Rows[i]["TINHTHANH"].ToString();
            dh.Quanhuyen = dt.Rows[i]["QUANHUYEN"].ToString();
            dh.Idsanpham = dt.Rows[i]["IDSANPHAM"].ToString();
            dh.Soluong = int.Parse(dt.Rows[i]["SOLUONG"].ToString());
            dh.Tonggia = float.Parse(dt.Rows[i]["TONGGIA"].ToString());
            dh.Hinhthucnhanhang = dt.Rows[i]["HINHTHUCNHANHANG"].ToString();

            return (object)dh;
        }

        public void insert(DonHangDTO info)
        {
            string insertCommand = "INSERT INTO DONHANG VALUES ('" +
                info.Iddonhang + "','" +
                info.Idthanhtoan + "','" +
                info.Diachinhan + "','" +
                info.Duong + "','" +
                info.Tinhthanh + "','" +
                info.Quanhuyen + "','" +
                info.Idsanpham + "'," +
                info.Soluong + "," +
                info.Tonggia + ",'" +
                info.Hinhthucnhanhang + "')";


            executeNonQuery(insertCommand);
        }

        public void update(DonHangDTO info)
        {
            string updateCommand = "UPDATE DONHANG " +
                                    "SET IDTHANHTOAN = '" + info.Idthanhtoan + "', " +
                                    " DIACHINHAN = '" + info.Diachinhan + "'," +
                                    " DUONG = '" + info.Duong + "'," +
                                    " TINHTHANH = '" + info.Tinhthanh + "'," +
                                    " QUANHUYEN = '" + info.Quanhuyen + "'," +
                                    " IDSANPHAM = '" + info.Idsanpham + "'," +
                                    " SOLUONG = " + info.Soluong + "," +
                                    " TONGGIA = " + info.Tonggia + "," +
                                    " HINHTHUCNHANHANG = '" + info.Hinhthucnhanhang + "'," +

                                    " WHERE IDDONHANG  = '" + info.Iddonhang + "'";

            executeNonQuery(updateCommand);
        }

        public void delete(string iddh)
        {
            string deleteCommand = "DELETE FROM DONHANG WHERE IDDONHANG = '" + iddh + "'";
            executeNonQuery(deleteCommand);
        }
    }
}